"""
进度 | 数据层
"""

from models.other import ProcessHistory
from models.po import ProcessPO
from service.base import ServiceBase
from utils.mock_utils import DATETIME


class ProcessService(ServiceBase[ProcessPO]):
    """ 进度 | 数据层 """

    po = ProcessPO
    collection_name = "common_process"
    single_field_index = [
        "id",
        "user_id",
        # "task",
    ]

    @staticmethod
    def __get_history(po: ProcessPO) -> ProcessHistory:
        """
        获取历史记录

        - percent: 进度百分比
        - update_time: 更新时间
        """

        return ProcessHistory(**po.model_dump())

    @classmethod
    def insert_one_with_history(cls, po: ProcessPO) -> None:
        """ 新增单个文档（带历史记录） """

        po.history = [cls.__get_history(po)]
        cls.insert_one(po)

    @classmethod
    def update_one_with_history(cls, po: ProcessPO) -> None:
        """ 更新单个文档（带历史记录） """

        po.update_time = DATETIME.now()
        po.history.append(cls.__get_history(po))
        cls.update_one(po)
